package com.beerq.view.friends;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beerq.R;
import com.beerq.view.BaseActivity;
import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;
import com.lzy.imagepicker.bean.ImageItem;

import java.util.ArrayList;

import static com.beerq.R.id.txt_index;
import static com.beerq.view_manager.AddImageManager.mListImgs;

public class ImageLookingActivity extends BaseActivity {

    private static final String STATE_POSITION = "STATE_POSITION";
    public static final String EXTRA_IMAGE_INDEX = "image_index";
    public static final String EXTRA_IMAGE_URLS = "image_urls";

    private HackyViewPager mPager;
    private int initPagerPosition;
    private static int currentPagerPosition;

    private TextView mTxtIndex;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_detail_pager);

        mTxtIndex=(TextView)findViewById(txt_index);

        initPagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);

        currentPagerPosition=initPagerPosition;
        setIndexTitle();

        mPager = (HackyViewPager) findViewById(R.id.pager);

        mPager.setAdapter(new SamplePagerAdapter(mListImgs));





        mPager.setCurrentItem(initPagerPosition);



        setEvent();

    }

    private void setIndexTitle(){
        mTxtIndex.setText((currentPagerPosition+1)+"/"+ mListImgs.size());
    }

    private void setEvent(){
        findViewById(R.id.txt_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        View delete=findViewById(R.id.img_delete);

        if(getIntent().getBooleanExtra("canNotDelete",false)){
            delete.setVisibility(View.GONE);
            findViewById(R.id.txt_index).setVisibility(View.GONE);
        }else {
            delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


//                    Bitmap bitmap=AddImageManager.imageCache.get(AddImageManager.mListImgs.get(currentPagerPosition).path).get();
//
//                    if(bitmap!=null){
//                        bitmap.recycle();
//                        bitmap=null;
//                    }


                    mListImgs.remove(currentPagerPosition);




                    mPager.setAdapter(new SamplePagerAdapter(mListImgs));
                    setIndexTitle();

                    if(mListImgs.size()==0){
                        finish();
                        return;
                    }
                }
            });
        }




        // 更新下标
        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageSelected(int arg0) {
                currentPagerPosition=arg0;
                setIndexTitle();
            }

        });
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(STATE_POSITION, mPager.getCurrentItem());
    }

    static class SamplePagerAdapter extends PagerAdapter {

        private ArrayList<ImageItem> mStringArrayList;

        public SamplePagerAdapter(ArrayList<ImageItem> list){
            mStringArrayList=list;
        }

        @Override
        public int getCount() {
            return mStringArrayList.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            //定义可以缩放的图片对象
            PhotoView photoView = new PhotoView(container.getContext());

            Glide.with(container.getContext()).load(mStringArrayList.get(position).path).into(photoView);
            currentPagerPosition=position;
            // Now just add PhotoView to ViewPager and return it
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
}